package io.github.yidasanqian.content.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.github.yidasanqian.bean.DozerBeanMappingBuilder;
import io.github.yidasanqian.content.service.ContentService;
import io.github.yidasanqian.domain.Content;
import io.github.yidasanqian.dto.ContentDTO;
import io.github.yidasanqian.mapper.ContentMapper;
import io.github.yidasanqian.vo.ContentVO;
import org.dozer.Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Linyu Chen
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ContentServiceImpl implements ContentService {
    @Resource
    private ContentMapper contentMapper;

    @Override
    public int addContent(ContentDTO contentDTO) {
        Mapper mapper = new DozerBeanMappingBuilder(ContentVO.class, Content.class).getDozerMapper();
        ContentVO contentVO = contentDTO.getContentVO();
        Content content = mapper.map(contentVO, Content.class);
        Date date = new Date();
        content.setCreated(date);
        content.setUpdated(date);
        return contentMapper.insert(content);
    }

    @Override
    public int deleteContent(String ids) {
        String[] idList = ids.split(",");
        int row = 0;
        for (String id : idList) {
            // 删除内容
            contentMapper.deleteByPrimaryKey(Long.valueOf(id));
            row++;
        }
        return row;
    }

    @Override
    public int updateContent(ContentDTO contentDTO) {
        Mapper mapper = new DozerBeanMappingBuilder(ContentVO.class, Content.class).getDozerMapper();
        ContentVO contentVO = contentDTO.getContentVO();
        Content content = mapper.map(contentVO, Content.class);
        Date date = new Date();
        content.setUpdated(date);
        return contentMapper.updateByPrimaryKeySelective(content);
    }

    @Override
    public ContentDTO getContent(long id) {
        Content content = contentMapper.selectByPrimaryKey(id);
        Mapper mapper = new DozerBeanMappingBuilder(Content.class, ContentVO.class).getDozerMapper();
        ContentVO contentVO = mapper.map(content, ContentVO.class);
        ContentDTO contentDTO = new ContentDTO();
        contentDTO.setContentVO(contentVO);
        return contentDTO;
    }

    @Override
    public ContentDTO queryContentList(long categoryId, int page, int rows) {
        Content record = new Content();
        record.setCategoryId(categoryId);
        PageHelper.startPage(page, rows);
        List<Content> contentList = contentMapper.select(record);
        PageInfo<Content> pageInfo = new PageInfo<>(contentList);
        Mapper mapper = new DozerBeanMappingBuilder(Content.class, ContentVO.class).getDozerMapper();
        List<ContentVO> contentVOList = new ArrayList<>();
        mapper.map(contentList, contentVOList);
        ContentDTO contentDTO = new ContentDTO();
        contentDTO.setTotal(pageInfo.getTotal());
        contentDTO.setContentVOList(contentVOList);
        return contentDTO;
    }
}
